package com.linkstec.bookshop.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.linkstec.bookshop.pojo.Scart;
import com.linkstec.bookshop.pojo.User;
import com.linkstec.bookshop.service.CartService;
import com.linkstec.bookshop.vo.ScartVo;

/**
 * 
 * @ClassName: ScartController 购物车的controller
 * @Description:TODO
 * @author: JGQiang
 * @date: 2018年11月26日 上午11:09:58
 */
@Controller
@RequestMapping("cart")
public class ScartController {

	@Autowired
	private CartService cartService;

	/**
	 * 
	 * @Description:TODO 添加购物车并跳转到购物车详情页
	 * @author: JGQiang
	 * @date: 2018年11月26日 上午11:10:15
	 * @param scart
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "addCart")
	public String addCart(Scart scart, HttpServletRequest req) {
		User user = (User) req.getSession().getAttribute("loginUser");
		scart.setUserId(user.getUserId());
		try {
			cartService.addCart(scart);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "redirect:/cart/toCart";
	}

	/**
	 * 
	 * @Description:TODO 查询并跳转到购物车页面
	 * @author: JGQiang
	 * @date: 2018年11月26日 上午11:23:31
	 * @param scart
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "toCart")
	public ModelAndView toCart(HttpServletRequest req) {
		ModelAndView mv = new ModelAndView();
		User user = (User) req.getSession().getAttribute("loginUser");
		List<ScartVo> cartList = null;
		double sumMoney =0;
		try {
			cartList = cartService.findUserCart(user.getUserId());
			
			for(int i = 0;i<cartList.size();i++){
				sumMoney += cartList.get(i).getPrice()*cartList.get(i).getNumber();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("cartList", cartList);
		req.getSession().setAttribute("cartList", cartList);
		mv.addObject("sumMoney", sumMoney);
		mv.setViewName("cart");
		return mv;
	}
	
	/**
	 * 
	 * @Description:TODO 删除单挑购物车信息并返回到购物城页面
	 * @author: JGQiang
	 * @date:   2018年11月26日 下午2:18:38    
	 * @param req
	 * @param scId
	 * @return
	 */
	@RequestMapping("delOneCart")
	public String delOneCart(HttpServletRequest req,int scId){
		User user = (User) req.getSession().getAttribute("loginUser");
			try {
				cartService.delOneCart(scId,user.getUserId());
			} catch (Exception e) {
				e.printStackTrace();
			}
		
		return "redirect:/cart/toCart";
	}
	
	/**
	 * 
	 * @Description:TODO 清空购物车
	 * @author: JGQiang
	 * @date:   2018年11月26日 下午2:19:55    
	 * @param req
	 * @return
	 */
	@RequestMapping("clearCart")
	public String clearCart(HttpServletRequest req){
		User user = (User) req.getSession().getAttribute("loginUser");
			try {
				cartService.clearCart(user.getUserId());
			} catch (Exception e) {
				e.printStackTrace();
			}
		
		return "redirect:/cart/toCart";
	}
	

}
